#ifdef	TEXTURED
uniform sampler2D tex;
#endif

varying	vec3 vNormal;
varying	vec4 vPos;
varying	vec3 vPosM;

#ifdef	SHADOW
uniform sampler2D rsmDepthTex;
uniform sampler2D rsmColorTex;
varying vec4 vShadowCoord;

uniform float mrtAmbient		= 0.2;
uniform float mrtShadowLight	= 0.6;

uniform float mrtShadowOffset	= 0.0003;
uniform float mrtShadowAmp	= 0.2;
#endif

void main()
{
	vec4	color = vec4(1.0,1.0,1.0,1.0);

#ifdef	VCOLOR
	color *= gl_Color;
#endif

#ifdef	TEXTURED
	color *= texture2D( tex, gl_TexCoord[0].st );
#endif

#ifdef	SHADOW
	vShadowCoord /= vShadowCoord.w;
	float shadow_d = texture2D( rsmDepthTex, vShadowCoord.st ).z;
	float light_factor = texture2D( rsmColorTex, vShadowCoord.st ).a;
	//vShadowCoord.z -= 0.0005;
	float diff = shadow_d - vShadowCoord.z;
	//float shadow_coef = clamp( diff * 500.0 + 1.0, 0.0, 1.0 );// * light_factor;
	//float shadow_coef = diff > -0.0005 ? 1.0 : 0.0;
	float shadow_coef = clamp( (diff + mrtShadowOffset) * mrtShadowAmp * 10000 + 0.5, 0.0, 1.0);
	float fact = mrtAmbient + mrtShadowLight * shadow_coef * light_factor;
	//color *= fact;
	color.a = fact;
#endif

#ifdef	MRT
	vec4 normal;
	normal.xyz = (vNormal * 0.5) + 0.5;
	
	vec4 pos;
//	pos.xyz = vPos.xyz / vPos.w;
//	pos.xy = (pos.xy * 0.5) + 0.5;
//	pos.xyz = vPosM.xyz / vPosM.w;
//	pos.xyz = (pos.xyz * 0.01) + 0.5;
	pos.xyz = vPosM;
	pos.xyz = (pos.xyz * 0.025) + 0.5;

#ifdef	SHADOW
	pos.w = vShadowCoord.s;
	normal.w = vShadowCoord.t;
#else
	//to be used
	normal.w = 0.0;
	pos.w = 0.0;
#endif

	
	gl_FragData[0] = color;
	gl_FragData[1] = pos;
	gl_FragData[2] = normal;
#else
	gl_FragColor = color;
#endif
}
